package com.yunhe.tianhe.rentcars.service.client.Impl;/**
 * Created by Administrator on 2017/11/23 0023.
 */

import com.yunhe.tianhe.commons.dto.Pageable;
import com.yunhe.tianhe.rentcars.dao.client.ClientDao;
import com.yunhe.tianhe.rentcars.entity.client.Client;
import com.yunhe.tianhe.rentcars.entity.client.ClientCompany;
import com.yunhe.tianhe.rentcars.entity.system.MemberLevel;
import com.yunhe.tianhe.rentcars.service.client.ClientService;
import com.yunhe.tianhe.rentcars.service.system.MemberLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @user yanxiu
 * @creat 2017-11-23 上午 11:00
 * @desc
 **/
@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientDao clientDao;
    @Autowired
    private MemberLevelService memberLevelService;

    @Override
    public void save(Client client, ClientCompany clientCompany) {
        clientDao.saveCompany(clientCompany); //保存公司

        Long companyId = clientCompany.getId(); //获取公司自增Id
        client.setCompanyId(companyId);
        List<MemberLevel> memberLevels = memberLevelService.findList();
        for(MemberLevel member:memberLevels){   //修改会员级别
            int minScore = member.getMinScore();
            int maxScore = member.getMaxScore();
            if(maxScore>0 && 0>=minScore){
                client.setMemLevel(member.getLevelId());
            }
        }
        clientDao.saveClient(client); //保存客户基本信息
    }

    @Override
    public Pageable<Client> searchList(Integer offset, Integer limit, Long memLevel, String addTime, String mobile, String lastTime) {

        Date lastDateTime=null;
        if(null!=lastTime && lastTime.equals("")){   //处理截止时间
            lastTime=null;
        }
        if(null!=lastTime) {
            lastTime = lastTime + " 23:59:59";
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                lastDateTime = dateFormat.parse(lastTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if(null!=addTime && addTime.equals("")){ //处理添加时间
            addTime=null;
        }


        List<Client> data = clientDao.searchList(offset, limit, memLevel, addTime, mobile, lastTime);
        if(null!=data){
            for(Client item:data){
                Long count = clientDao.rentCount(item.getId());//添加客户租车次数
                item.setRentCount(count);
            }
        }

        Pageable<Client> pageable = new Pageable<>();
        Long count = clientDao.countLines(offset, limit, memLevel, addTime, mobile, lastTime);
        pageable.setPage(offset / limit + 1);
        pageable.setSize(limit);
        pageable.setTotal(count);
        pageable.setRows(data);
        return pageable;
    }


    @Override
    public Client findOneByMobile(String mobile) {
        Client client = clientDao.findOneByMobile(mobile);
        if(null!=client){
            Long count = clientDao.rentCount(client.getId()); //添加客户租车次数
            client.setRentCount(count);
        }
        return client;
    }

}
